package com.android.nurse;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class LogInScreen extends CommonActivity {

	private EditText editUserName;
	private Spinner spinnerAge;
	private Button btnStart;
	private Button btnBack;
	private ArrayList<AgeMaster> listAge;
	private int noAge;
	private int index;
	public static final String userName = "USER_NAME";
	public static final String ageNurse = "AGE_NURSE";
	public static final String Last_UserName = "LAST_USER_NAME";
	public static final String Last_Age = "LAST_AGE";
	private String lastuserName;
	private int lastAge;
	private int width;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		LocalRanking.addedNew = false;
		super.onCreate(savedInstanceState);
		Display display = ((WindowManager) this
				.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		width = display.getWidth();
		Log.d("chieu rong man hinh", ""+width);
		if(width > 500){
			setContentView(R.layout.login_big);
		} else {
		setContentView(R.layout.login);
		}
		listAge = getAgeMaster();
		editUserName = (EditText) findViewById(R.id.editUserName);
		spinnerAge = (Spinner) findViewById(R.id.spinnerAge);
		btnStart = (Button) findViewById(R.id.btnStart);
		btnBack = (Button) findViewById(R.id.btnBack);
		myThread.start();
		getInfoLastUser();
		ArrayAdapter<AgeMaster> adapter = new ArrayAdapter<AgeMaster>(this, 
				android.R.layout.simple_spinner_item, listAge);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		editUserName.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				if( (arg0.length() > 0) && (index > 0)){
					btnStart.setBackgroundResource(R.drawable.btn_start);
				} else {
					btnStart.setBackgroundResource(R.drawable.btn_start_disable);
				}
			}
		});
		spinnerAge.setAdapter(adapter);
		if((!lastuserName.equals("no exits!")) && (lastAge != 0)){
			editUserName.setText(lastuserName);
			spinnerAge.setSelection(getIndex(listAge, lastAge));
		}
		/*
		 * event select an item of spinner
		 */
		spinnerAge.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				index = spinnerAge.getSelectedItemPosition();
				AgeMaster age = (AgeMaster) spinnerAge.getSelectedItem();
				noAge = age.getKey();
				if( (index > 0) && (editUserName.getText().toString().length() > 0)){
					btnStart.setBackgroundResource(R.drawable.btn_start);
				} else {
					btnStart.setBackgroundResource(R.drawable.btn_start_disable);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		/*
		 * event click on Start button
		 */
		btnStart.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction() & MotionEvent.ACTION_MASK) {

				case MotionEvent.ACTION_DOWN:
					Log.d("touchhhh", "action dowm");
					if ((editUserName.getText().toString().trim().length() > 0)
							&& (index > 0)){
					btnStart.setBackgroundResource(R.drawable.btn_start_2);
					} 
					break;
				case MotionEvent.ACTION_UP:
					if ((editUserName.getText().toString().trim().length() != 0)
							&& (index != 0)) {
						btnStart.setBackgroundResource(R.drawable.btn_start);
						saveInfoLastUser();
						QuestionsScreen.resetData();
						Intent i = new Intent(getApplicationContext(),
								QuestionsScreen.class);
						customeActivity(i);
						sendData();
					}
					break;
				}
				return true;
			}
		});
		
		/*
		 * event click on Back button
		 */
		btnBack.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction() & MotionEvent.ACTION_MASK) {

				case MotionEvent.ACTION_DOWN:
					if(width > 500){
						btnBack.setBackgroundResource(R.drawable.btn_back_2_big);
					} else {
					btnBack.setBackgroundResource(R.drawable.btn_back_2);
					}
					break;
				case MotionEvent.ACTION_UP:
					if(width > 500){
						btnBack.setBackgroundResource(R.drawable.btn_back_big);
					} else {
					btnBack.setBackgroundResource(R.drawable.btn_back);
					}
					Intent i = new Intent(getApplicationContext(), TopScreen.class);
					backScreen(i);
					break;
				}
				return true;
			}
		});
	}

	/*
	 * send username and age of Nurse
	 */
	public void sendData() {
		Log.d("user",editUserName.getText().toString().trim());
		SharedPreferences settings = getSharedPreferences(
				QuestionsScreen.PREFS_NAME, 0);
		SharedPreferences.Editor edit = settings.edit();
		edit.putString(userName, editUserName.getText().toString().trim());
		edit.putInt(ageNurse, noAge);
		edit.putInt("VIEWED", 1);
		edit.commit();
	}
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		//TopScreen.updateTimes = 0;
	}
	
	/*
	 * Save info about last user
	 */
	public void saveInfoLastUser() {
		SharedPreferences settings = getSharedPreferences(
				QuestionsScreen.PREFS_NAME, 0);
		SharedPreferences.Editor edit = settings.edit();
		edit.putString(Last_UserName, editUserName.getText().toString().trim());
		edit.putInt(Last_Age, noAge);
		edit.commit();
	}
	/*
	 * get info last user
	 */
	public void getInfoLastUser(){
		SharedPreferences settings = getSharedPreferences(QuestionsScreen.PREFS_NAME, 0);
		lastuserName = settings.getString(Last_UserName, "no exits!");
		lastAge = settings.getInt(Last_Age, 0);
	}
	/*
	 * find index of age in list Age
	 */
	private int getIndex(ArrayList<AgeMaster> listAge, int lastAge){
		int index = 0;
		for(int i = 0; i < listAge.size(); i++){
			if(lastAge == listAge.get(i).getKey()){
				index = i;
				break;
			}
			
		}
		return index;
	}
	@Override
	public void onBackPressed() {
	   return;
	}
	
private Thread myThread = new Thread(new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			btnBack.setClickable(false);
			synchronized (this) {
				try {
					wait(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			btnBack.setClickable(true);
		}
	});

@Override
protected void onResume() {
	// TODO Auto-generated method stub
	super.onResume();
}

}
